#!/usr/bin/env python
# This Python file uses the following encoding: utf-8


import pygame
from pygame.sprite import Group
from settings import Settings
from game_stats import GameStats
from ship import Ship
from button import Button
from scoreboard import Scoreboard
import game_function as gf


def run_game():
    # 初始化游戏并创建一个屏幕对象
    # 初始化背景设置
    pygame.init()

    ai_settings = Settings()
    # 窗口大小
    screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
    # 窗口名称
    pygame.display.set_caption('Alien Invasion')

    play_button = Button(ai_settings, screen, 'Play')

    stats = GameStats(ai_settings)

    sb = Scoreboard(ai_settings, screen, stats)

    # 创建飞船
    ship = Ship(ai_settings, screen)
    bullets = Group()

    # bulid alien
    aliens = Group()
    gf.create_fleet(ai_settings, screen, ship, aliens)

    # 开始游戏
    while True:

        gf.check_events(ai_settings, screen, stats, play_button, ship, aliens, bullets)

        if stats.game_active:
            ship.update()

            gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets)
            gf.update_aliens(ai_settings, stats, screen, ship, aliens, bullets)

        gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button)


run_game()

